package indi.xliuovo.coursedesign.musicplayer;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

/**
 * Created with Android Studio
 *
 * @Author xliuOvO
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    TextView login, register;
    LoginFragment loginFragment;
    RegisterFragment registerFragment;
    QuestionFragment questionFragment;
    CustomVideoView customVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        login.setTextColor(this.getResources().getColor(R.color.teal_200));
        getSupportFragmentManager().beginTransaction().add(R.id.container, loginFragment).commit();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    void initView() {
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);
        // 注册点击事件
        login.setOnClickListener(this);
        register.setOnClickListener(this);

        loginFragment = new LoginFragment();
        registerFragment = new RegisterFragment();
        questionFragment = new QuestionFragment();

        // 找VideoView控件
        customVideoView = findViewById(R.id.wallpaper);
        // 加载视频文件
        customVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/raw/" + R.raw.ship));
        // 播放
        customVideoView.start();
        // 循环播放
        customVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                customVideoView.start();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                changeColor(v.getId());
                getSupportFragmentManager().beginTransaction().remove(registerFragment).commit();
                getSupportFragmentManager().beginTransaction().replace(R.id.container, loginFragment).commit();
                break;
            case R.id.register:
                changeColor(v.getId());
                getSupportFragmentManager().beginTransaction().remove(loginFragment).commit();
                getSupportFragmentManager().beginTransaction().replace(R.id.container, registerFragment).commit();
                break;
        }
    }

    // 改变文本颜色
    void changeColor(int viewId) {
        if (viewId == R.id.login) {
            login.setTextColor(this.getResources().getColor(R.color.teal_200));
            register.setTextColor(this.getResources().getColor(R.color.grey));
        } else if (viewId == R.id.register) {
            register.setTextColor(this.getResources().getColor(R.color.teal_200));
            login.setTextColor(this.getResources().getColor(R.color.grey));
        }
    }
}